﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_BorrowingDetails : Common
    {

        #region Borrowing Details

        public List<FrontUserBorrowingDetailModel> GetFrontDataList(int borrowingId = 0)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_BorrowingDetail> queryBorrowDetail = _context.Library_BorrowingDetails;
            //Filter
            if (borrowingId > 0)
            {
                queryBorrowDetail = queryBorrowDetail.Where(bd => bd.BorrowingId == borrowingId);
            }
            List<Library_BorrowingDetail> borrows = queryBorrowDetail.ToList();
            List<FrontUserBorrowingDetailModel> borrowList = new List<FrontUserBorrowingDetailModel>();

            foreach (Library_BorrowingDetail b in borrows)
            {
                FrontUserBorrowingDetailModel data = new FrontUserBorrowingDetailModel();
                data.ID = b.Id;
                data.BorrowingID = b.BorrowingId.ToString();
                data.BookName = b.Library_Book.Name;
                data.BookCode = b.Library_Book.Code;
                data.Quantity = b.Quantity;
                borrowList.Add(data);
            }
            _context.Dispose();
            return borrowList;
        }

        public List<BorrowingDetailModel> GetDataList(int borrowingId = 0, int startIndex = 0, int pageSize = 0, string sorting = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            IEnumerable<Library_BorrowingDetail> queryBorrowDetail = _context.Library_BorrowingDetails;
            //Filter
            if (borrowingId > 0)
            {
                queryBorrowDetail = queryBorrowDetail.Where(bd => bd.BorrowingId == borrowingId);
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("BorrowingID ASC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderBy(bd => bd.BorrowingId);
            }
            else if (sorting.Equals("BorrowingID DESC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderByDescending(bd => bd.BorrowingId);
            }
            else if (sorting.Equals("BookID ASC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderBy(bd => bd.Library_Book.Name);
            }
            else if (sorting.Equals("BookID DESC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderByDescending(bd => bd.Library_Book.Name);
            }
            else if (sorting.Equals("Quantity ASC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderBy(bd => bd.Quantity);
            }
            else if (sorting.Equals("Quantity DESC"))
            {
                queryBorrowDetail = queryBorrowDetail.OrderByDescending(bd => bd.Quantity);
            }
            else
            {
                queryBorrowDetail = queryBorrowDetail.OrderBy(bd => bd.Library_Book.Name);
            }
            //
            List<Library_BorrowingDetail> borrows = pageSize > 0 ?
                                                    queryBorrowDetail.Skip(startIndex).Take(pageSize).ToList()
                                                    : queryBorrowDetail.ToList();
            List<BorrowingDetailModel> borrowList = new List<BorrowingDetailModel>();

            foreach (Library_BorrowingDetail b in borrows)
            {
                BorrowingDetailModel data = new BorrowingDetailModel();
                data.ID = b.Id;
                data.BorrowingID = b.Library_Borrowing.Id;
                data.BookID = b.Library_Book.Id;
                data.Quantity = b.Quantity;
                borrowList.Add(data);
            }
            _context.Dispose();
            return borrowList;
        }

        public BorrowingDetailModel AddNewData(BorrowingDetailModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_BorrowingDetail borrowDetails = new Library_BorrowingDetail();
            //Assign Parameter
            borrowDetails.BorrowingId = data.BorrowingID;
            borrowDetails.BookId = data.BookID;
            borrowDetails.Quantity = data.Quantity;
            //Insert into database
            _context.Library_BorrowingDetails.InsertOnSubmit(borrowDetails);
            _context.SubmitChanges();
            //Assign new ID to Model
            data.ID = borrowDetails.Id;
            //
            _context.Dispose();
            return data;
        }

        public bool UpdateData(BorrowingDetailModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_BorrowingDetail borrowDetails = _context.Library_BorrowingDetails.Where(bd => bd.Id == data.ID).FirstOrDefault();
            //Assign Parameter
            borrowDetails.BorrowingId = data.BorrowingID;
            borrowDetails.BookId = data.BookID;
            borrowDetails.Quantity = data.Quantity;
            //Update
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_BorrowingDetail borrowDetails = _context.Library_BorrowingDetails.Where(bd => bd.Id == Id).FirstOrDefault();
            //Delete
            _context.Library_BorrowingDetails.DeleteOnSubmit(borrowDetails);
            _context.SubmitChanges();
            //
            _context.Dispose(); ;
            return true;
        }
        #endregion

    }
}